package com.quectel.cms.controller.propertybill;


import com.quectel.business.common.propertybill.PropertyBillBusiness;
import com.quectel.business.common.util.sys.SysCodeUtils;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.constant.sys.SysCodeKeyConstants;
import com.quectel.core.module.propertybill.dto.PropertyChargeableListActivityDto;
import com.quectel.core.module.propertybill.service.PropertyChargeableItemService;
import com.quectel.core.module.propertybill.service.PropertyChargeableListActivityService;
import com.quectel.sys.module.sys.dto.SysCodeDto;
import com.quectel.util.common.JacksonUtils;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/03/31 14:36
 */
@RestController
@RequestMapping("propertyChargeableListActivity")
@Tag(name = "PropertyChargeableListActivityController", description = "物业收费项目优惠活动配置相关api")
public class PropertyChargeableListActivityController extends BaseController {

    @DubboReference
    private PropertyChargeableListActivityService propertyChargeableListActivityService;
    @DubboReference
    private PropertyChargeableItemService propertyChargeableItemService;

    @Autowired
    private PropertyBillBusiness propertyBillBusiness;


    @GetMapping("queryByPeriod")
    public Response<List<SysCodeDto>> queryByPeriod(@RequestParam(required = false) String checkPeriod) {

        List<SysCodeDto> changingStandardCycleType = SysCodeUtils.easyToGetByMark("ChangingStandardCycleType");
        List<SysCodeDto> children = new ArrayList<>();
        for (SysCodeDto sysCodeDto : changingStandardCycleType) {
            if (checkPeriod.equals(sysCodeDto.getValue())) {
                children = sysCodeDto.getChildren();
            }
        }

        return Response.<List<SysCodeDto>>ok().wrap(children);
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "propertyChargeableListActivity:list")
    @Operation(summary = "分页获取物业收费项目优惠活动配置列表")
    public Response<Page<PropertyChargeableListActivityDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = propertyChargeableListActivityService.queryTotal(request.getParams());

        List<PropertyChargeableListActivityDto> list = null;
        if (total > 0) {
            list = propertyChargeableListActivityService.queryList(request.getParams());
            for (PropertyChargeableListActivityDto activityDto : list) {
                Byte period = activityDto.getPeriod();
                if (activityDto.getTimeArr()!= null){
                    List<String> stringList = JacksonUtils.parseArray(activityDto.getTimeArr(), String.class);
                    List<SysCodeDto> changingStandardCycleType = SysCodeUtils.easyToGetByMark(SysCodeKeyConstants.CHANGING_STANDARD_CYCLE_TYPE);
                    if (CollectionUtils.isNotEmpty(changingStandardCycleType)) {
                        for (int i = 0; i < changingStandardCycleType.size(); i++) {
                            if (changingStandardCycleType.get(i).getValue().equals(period + "")) {
                                List<SysCodeDto> children = changingStandardCycleType.get(i).getChildren();
                                StringBuilder builder = new StringBuilder();
                                for (SysCodeDto sysCode : children) {
                                    for (int j = 0; j < stringList.size(); j++) {
                                        if (sysCode.getValue().equals(stringList.get(j))) {
                                            if (builder.length() > 0) {
                                                builder.append(",");
                                            }
                                            builder.append(sysCode.getName());
                                        }
                                    }
                                }
                                activityDto.setPeriodTimeArrString(builder.toString());
                            }
                        }
                    }
                }
            }
        } else {
            list = new ArrayList<>();
        }

        Page<PropertyChargeableListActivityDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PropertyChargeableListActivityDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "propertyChargeableListActivity:edit")
    @Operation(summary = "在编辑时根据id查询物业收费项目优惠活动配置详情")
    public Response<PropertyChargeableListActivityDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (propertyChargeableListActivityService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PropertyChargeableListActivityDto propertyChargeableListActivityDto = propertyChargeableListActivityService.selectById(id);

        return Response.<PropertyChargeableListActivityDto>ok().wrap(propertyChargeableListActivityDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "propertyChargeableListActivity:info")
    @Operation(summary = "根据id查询物业收费项目优惠活动配置的详情")
    public Response<PropertyChargeableListActivityDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (propertyChargeableListActivityService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PropertyChargeableListActivityDto propertyChargeableListActivityDto = propertyChargeableListActivityService.selectById(id);
        //填充项目名
        propertyChargeableListActivityDto = propertyBillBusiness.fillInProjectName(propertyChargeableListActivityDto);

        return Response.<PropertyChargeableListActivityDto>ok().wrap(propertyChargeableListActivityDto);
    }

    /**
     * 保存
     *
     * @param propertyChargeableListActivityDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "propertyChargeableListActivity:edit")
    @Operation(summary = "新增物业收费项目优惠活动配置")
    public Response<Object> save(@RequestBody @Validated PropertyChargeableListActivityDto propertyChargeableListActivityDto) {

        //填充基础数据
        fillSaveCommonData(propertyChargeableListActivityDto);

        propertyChargeableListActivityService.save(propertyChargeableListActivityDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param propertyChargeableListActivityDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "propertyChargeableListActivity:edit")
    @Operation(summary = "根据id修改物业收费项目优惠活动配置")
    public Response<Object> update(@RequestBody @Validated PropertyChargeableListActivityDto propertyChargeableListActivityDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(propertyChargeableListActivityDto.getId()));
        if (propertyChargeableListActivityService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(propertyChargeableListActivityDto);

        propertyChargeableListActivityService.updateById(propertyChargeableListActivityDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "propertyChargeableListActivity:delete")
    @Operation(summary = "根据多条记录id批量删除物业收费项目优惠活动配置")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (propertyChargeableListActivityService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        propertyChargeableListActivityService.deleteBatch(ids);

        return Response.ok();
    }
}
